<template>
  <div class="employees-detail-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账号设置">
            <!-- 放置表单 -->
            <el-form ref="userRef" label-width="120px" :rules="rules" style="margin-left: 120px; margin-top:30px" :model="userInfo">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="newPassword">
                <el-input v-model="userInfo.newPassword" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="update">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!-- 内容 -->
            <UserInfo />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <!-- 内容 -->
            <JobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import UserInfo from './components/user-info.vue'
import JobInfo from './components/job-info.vue'
import { reqGetUserImgById, reqUpdateUserInfo } from '@/api/user'
export default {
  name: 'EmployeesDetail',
  components: { UserInfo, JobInfo },
  data() {
    return {
      userInfo: {
        username: '',
        newPassword: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: ['change', 'blur'] }
        ],
        newPassword: [
          { min: 6, max: 9, message: '密码必须是6-9位', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  async created() {
    // console.log(this.$route.params.id)
    const { data: { data }} = await reqGetUserImgById(this.$route.params.id)
    // console.log(data)
    this.userInfo = data
  },
  methods: {
    update() {
      this.$refs.userRef.validate(async flag => {
        if (flag) {
          const res = await reqUpdateUserInfo({ ...this.userInfo, password: this.userInfo.newPassword })
          console.log(res)
          this.$message.success('更新成功')
          this.$router.back()
        }
      })
    }
  }
}
</script>
